const {ccclass, property} = cc._decorator;

import Sprite from "./sprite"
@ccclass
export default class NewClass extends cc.Component {

    @property(cc.Prefab)
    mapPrefab : cc.Prefab = null

    @property(cc.Node)
    hero: cc.Node = null
    // LIFE-CYCLE CALLBACKS:

    @property(cc.Prefab)
    bulletPrefab: cc.Prefab = null;

    @property(cc.Prefab)
    enemyPrefab: cc.Prefab = null;

    enemyFlashDuration: number = 1;
    enemyLastFlashedTimer: number = 0;

    fireDuration: number = 2;
    lastFiredTimer: number = 0;



    flashNewEnemy(){
        let enemy = cc.instantiate(this.enemyPrefab);
        this.node.addChild(enemy);
        let enemyPos = this.getNewEnemyPosition();
        enemy.setPosition(enemyPos);
    }

    getNewEnemyPosition(): cc.Vec2{
        let randX = this.node.width * cc.randomMinus1To1() / 2; 
        let randY = this.node.height / 2 - 50;
        return cc.p(randX, randY);
    }
    // Hero fire bullet
    fire(){
        let heroPos = this.hero.getPosition();
        let pos = cc.p(heroPos.x, heroPos.y+this.hero.getContentSize().height/2);
        let bullet = cc.instantiate(this.bulletPrefab);
        this.node.addChild(bullet);
        bullet.setPosition(pos);

        bullet.getComponent('bullet').main= this;
    }

    onLoad () {
        //Top map
        let screenSize = cc.director.getWinSize();

        let map1 = cc.instantiate(this.mapPrefab);
        this.node.addChild(map1,-1);
        map1.setPosition(0, screenSize.height);

        let map2 = cc.instantiate(this.mapPrefab);
        this.node.addChild(map2,-1);
        map2.setPosition(0, 0);

        // Set cross reference
        map1.getComponent('map').anotherMap = map2;
        map2.getComponent('map').anotherMap = map1;
    }

    isTouchOnHero(pos: cc.Vec2): boolean {
        let heroSize = this.hero.getContentSize();
        let heroPos = this.hero.getPosition();
        return !!(pos.x <= heroPos.x + heroSize.width / 2
            && pos.x >= heroPos.x - heroSize.width / 2
            && pos.y <= heroPos.y + heroSize.height / 2
            && pos.y >= heroPos.y - heroSize.height / 2)
    }

    start() {
        //Enable Collision Detect
        let Collisionmanager = cc.director.getCollisionManager();
        Collisionmanager.enabled = true;
        // Collisionmanager.enabledDebugDraw = true;

        let heroCtrl = this.hero.getComponent('hero');

        this.node.on(cc.Node.EventType.TOUCH_START, (event: cc.Event.EventTouch) => {
            let touchPos = this.hero.parent.convertToNodeSpaceAR(event.getLocation());
            if(this.isTouchOnHero(touchPos)){
                heroCtrl.setTouched(true);
            };
        }, this.node);

        this.node.on(cc.Node.EventType.TOUCH_MOVE, (event: cc.Event.EventTouch) => {
            if(heroCtrl.getTouched()){
            let touchPos = this.hero.parent.convertToNodeSpaceAR(event.getLocation());
                heroCtrl.moveTo(touchPos);
            }
        }, this.node);

        this.node.on(cc.Node.EventType.TOUCH_END, (event: cc.Event.EventTouch) => {
            if(heroCtrl.getTouched()){
                heroCtrl.setTouched(false);
            }
        }, this.node);
        this.fire();
        this.flashNewEnemy();
    }

    update(dt) {
        // Flash new enenmy
    if (this.enemyLastFlashedTimer >= this.enemyFlashDuration) {
            this.flashNewEnemy();
            this.enemyLastFlashedTimer = 0;
        } else{
            this.enemyLastFlashedTimer += dt;
        }
            // Fire
    if (this.lastFiredTimer >= this.fireDuration) {
            this.fire();
            this.lastFiredTimer = 0;
        } else{
            this.lastFiredTimer += dt;
        }
    }
 
}
